package com.centriole.service.workflow.service.parse.component;

import com.centriole.service.workflow.enums.FlowNode;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 *  功   能： 审批节点 封装对象, 调用toBpmnString()方法获取bpmn文件格式的字符串
 *  创建人：陈默晗
 *  时   间：2017年12月27日下午2:03:11
 */
public class ApprovalNode extends MultiTaskNode{

	private static AtomicInteger LINE_COUNTER = new AtomicInteger(0);		//连接线计数器,设置 id 时使用

	private  String allowTaskNodeId;      //审批同意的任务节点id
	private  String rejectTaskNodeId;      //审批不同意的任务节点id
	private  String apprResultName;		//审批节点的流程变量名
	
	public ApprovalNode() {}
	
    public ApprovalNode( String nodeId, String type,List<Property.Task> childTasks ,Property property){
        this.id = nodeId;
        this.type = type;
        this.property = property;
        entryNodeId =id;
        exitNodeId = id + "-exit";
        joinNodeId = id + "-join";
        allowTaskNodeId = id + "-allowTask";
        rejectTaskNodeId = id + "-rejectTask";
        apprResultName = id + "_result";
    }
    
    /**
     * 重写父类MultiTaskNode的toBpmnString()方法
     * @return String bpmn文件格式的字符串
     */
    @Override
    public String toBpmnString(){
        //非空检查
        if(StringUtils.isBlank(id) ) return null;
        StringBuilder bpmn = new StringBuilder();

        //拼装内部节点
        bpmn.append( "<parallelGateway gatewayDirection=\"Unspecified\" id=\""+entryNodeId+"\" name=\""+"ParallelGateway"+"\"/>" );
      
        	//内部子任务(同意or不同意)
    	if( FlowNode.FKUSER.equals(  getProperty().getFkType()) ){
   	     	bpmn.append("<userTask id=\""+allowTaskNodeId+"\" name=\""+"同意子任务"+"\" activiti:candidateUsers=\""+isCurrentUserAndreplace( getProperty().getFkId() )+"\"></userTask>");
  	     	bpmn.append("<userTask id=\""+ rejectTaskNodeId+"\" name=\""+"拒绝子任务"+"\" activiti:candidateUsers=\""+isCurrentUserAndreplace( getProperty().getFkId() )+"\"></userTask>");
    	}
    	if( FlowNode.FKGROUP.equals( getProperty().getFkType()) ) {
 		   bpmn.append("<userTask id=\""+allowTaskNodeId+"\" name=\""+"同意子任务"+"\" activiti:candidateGroups=\""+getProperty().getFkId()+"\"></userTask>");
 		   bpmn.append("<userTask id=\""+ rejectTaskNodeId+"\" name=\""+"拒绝子任务"+"\" activiti:candidateGroups=\""+getProperty().getFkId()+"\"></userTask>");
    	}

        bpmn.append( "<parallelGateway gatewayDirection=\"Unspecified\" id=\""+joinNodeId+"\" name=\""+"ParallelGateway"+"\"/>" );
        bpmn.append( "<exclusiveGateway gatewayDirection=\"Unspecified\" id=\""+exitNodeId+"\" name=\""+"ExclusiveGateway"+"\"/>" );

        //拼装内部连线
        bpmn.append("<sequenceFlow id=\""+"appr_line_"+(LINE_COUNTER.getAndIncrement())+"\" sourceRef=\""+entryNodeId+"\" targetRef=\""+allowTaskNodeId+"\"/>");
        bpmn.append("<sequenceFlow id=\""+"appr_line_"+(LINE_COUNTER.getAndIncrement())+"\" sourceRef=\""+entryNodeId+"\" targetRef=\""+rejectTaskNodeId+"\"/>");
        bpmn.append("<sequenceFlow id=\""+"appr_line_"+(LINE_COUNTER.getAndIncrement())+"\" sourceRef=\""+allowTaskNodeId+"\" targetRef=\""+joinNodeId+"\"/>");
        bpmn.append("<sequenceFlow id=\""+"appr_line_"+(LINE_COUNTER.getAndIncrement())+"\" sourceRef=\""+rejectTaskNodeId+"\" targetRef=\""+joinNodeId+"\"/>");
        bpmn.append("<sequenceFlow id=\""+"appr_line_"+(LINE_COUNTER.getAndIncrement())+"\" sourceRef=\""+joinNodeId+"\" targetRef=\""+exitNodeId+"\"/>");
        		//拼装内部出口条件连接线
        bpmn.append(" <sequenceFlow id=\""+"appr_line_"+(LINE_COUNTER.getAndIncrement())+"\" sourceRef=\""+exitNodeId+"\" targetRef=\""+getProperty().getAgreeNodeId()+"\">" +
                "<conditionExpression xsi:type=\"tFormalExpression\"><![CDATA[${"+apprResultName+"==1}]]></conditionExpression>" +
                "</sequenceFlow>");
        bpmn.append(" <sequenceFlow id=\""+"appr_line_"+(LINE_COUNTER.getAndIncrement())+"\" sourceRef=\""+exitNodeId+"\" targetRef=\""+getProperty().getDisagreeNodeId()+"\">" +
                "<conditionExpression xsi:type=\"tFormalExpression\"><![CDATA[${"+apprResultName+"==0}]]></conditionExpression>" +
                "</sequenceFlow>");
        
        //返回解析的字符串
        return bpmn.toString().replaceAll("&lt;", "<").replaceAll("&gt;", ">");
    }

}
